package com.autohammal.monitor.scheduler.support;

import java.util.concurrent.TimeUnit;

import com.autohammal.monitor.scheduler.implement.AdvanceScheduler;
import com.autohammal.monitor.scheduler.implement.SimpleMonitorScheduler;

public final class SchedulerUnit {
	private long first;// first start time
	private long period;// space time
	private long delay;
	private TimeUnit timeUnit = TimeUnit.SECONDS;
	private FixedType fixedType;

	protected SchedulerUnit() {
		this.fixedType = FixedType.RATE;
	}

	/**
	 * use for {@link AdvanceScheduler}
	 * 
	 * @param initialDelay
	 * @param delay
	 * @param timeUnit
	 * @return
	 */
	public static SchedulerUnit withAdvanceFixedDelay(long initialDelay,
			long delay, TimeUnit timeUnit) {
		SchedulerUnit sunit = new SchedulerUnit();
		sunit.setFirst(initialDelay);
		sunit.setDelay(delay);
		sunit.setTimeUnit(timeUnit);
		sunit.setFixedType(FixedType.DELAY);
		return sunit;
	}

	public static SchedulerUnit withAdvanceFixedDelay(long initialDelay,
			long delay) {
		return withAdvanceFixedDelay(initialDelay, delay, TimeUnit.SECONDS);
	}

	/**
	 * use for {@link SimpleMonitorScheduler}
	 * 
	 * @param first
	 *            first start time(MILLISECONDS)
	 * @param period
	 * @return SchedulerUnit
	 */
	public static SchedulerUnit atSimpleFixedRate(long first, long period) {
		SchedulerUnit sunit = new SchedulerUnit();
		sunit.setFirst(first);
		sunit.setPeriod(period);
		sunit.setTimeUnit(TimeUnit.MILLISECONDS);
		sunit.setFixedType(FixedType.RATE);
		return sunit;
	}

	/**
	 * use for {@link AdvanceScheduler}
	 * 
	 * @param initialDelay
	 *            delay this time to start
	 * @param period
	 * @param timeUnit
	 * @return
	 */
	public static SchedulerUnit atAdvanceFixedRate(long initialDelay,
			long period, TimeUnit timeUnit) {
		SchedulerUnit sunit = new SchedulerUnit();
		sunit.setFirst(initialDelay);
		sunit.setPeriod(period);
		sunit.setTimeUnit(timeUnit);
		sunit.setFixedType(FixedType.RATE);
		return sunit;
	}

	public static SchedulerUnit atAdvanceFixedRate(long initialDelay,
			long period) {
		return atAdvanceFixedRate(initialDelay, period, TimeUnit.SECONDS);
	}

	public final TimeUnit getTimeUnit() {
		return timeUnit;
	}

	public void setTimeUnit(TimeUnit timeUnit) {
		if (timeUnit == null)
			timeUnit = TimeUnit.SECONDS;
		this.timeUnit = timeUnit;
	}

	public long getPeriod() {
		return period;
	}

	public void setPeriod(long period) {
		this.period = period;
	}

	public long getFirst() {
		return first;
	}

	public void setFirst(long first) {
		this.first = first;
	}

	public long getDelay() {
		return delay;
	}

	public void setDelay(long delay) {
		this.delay = delay;
	}

	public FixedType getFixedType() {
		return fixedType;
	}

	public void setFixedType(FixedType fixedType) {
		this.fixedType = fixedType;
	}

}
